package queries;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Class for representing search results
 * 
 * @author Vika
 * @author ak2765
 */
public class QueryResult {

	private String nextResultUrl;
	
	private List<Document> documents = new LinkedList<Document>();

	public List<Document> getDocuments() {
		return documents;
	}

	public String getNextResultUrl() {
		return nextResultUrl;
	}

	public void setNextResultUrl(String nextResultUrl) {
		this.nextResultUrl = nextResultUrl;
	}

	public void addDocument(Document document) {
		this.documents.add(document);
	}
	
	@Override
	public String toString() {
		StringBuilder buff = new StringBuilder();
		buff.append("QueryResult[nextResultUrl=").append(this.nextResultUrl);
		
		buff.append(", pages=[");
		for(Document doc : this.documents) {
			buff.append("\n\t[");
			buff.append(doc.toString());
			buff.append("]");
		}
		
		return buff.toString();
	}

	/**
	 * @param documentIndexes
	 * @return
	 */
	public List<Document> selectResultsExcluding(int[] documentIndexes) {
		List<Document> excluded = new ArrayList<Document>(this.documents);
		
		Arrays.sort(documentIndexes);
		
		int currentExcludeIndex = 0;
		for(int i=0;i<this.documents.size();i++) {
			if (i == documentIndexes[currentExcludeIndex]) {
				if (currentExcludeIndex < documentIndexes.length-1) {
					currentExcludeIndex++;
				}
				continue;
			}
			
			excluded.add(this.documents.get(i));
		}
		
		return excluded;
	}

	/**
	 * @param documentIndexes
	 * @return
	 */
	public List<Document> selectResultsIncluding(int[] documentIndexes) {
		List<Document> selected = new ArrayList<Document>();
		
		Arrays.sort(documentIndexes);

		for(int includeIndex : documentIndexes) {
			selected.add(this.documents.get(includeIndex));
		}
		
		return selected;

	}
}
